package com.cssw.fyzb.busi.api.task;

import com.cssw.fyzb.busi.service.task.TaskService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: zzf
 * @time: 2021/1/22 15:22
 */
@Function(description = "执行每日任务观看广告", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "advert", type = Type.Integer, description = "广告标识", required = true)
})
@Component
public class ViewDailyTask implements Action {

    private final static Logger logger = LoggerFactory.getLogger(DailyTasks.class);

    @Autowired
    private TaskService taskService;

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Override
    public Response execute(Request request) throws Exception {
        String advert = request.getString("advert");
        Long userId = request.getLong("userId");
        SimpleLock lock = simpleLockFactory.getLock("VIEWDAILYTASK_TASK_" + userId);
        try {
            lock.acquire();
            taskService.viewDailyTask(userId, advert);
            return Response.success();
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        } finally {
            lock.release();
        }
    }
}
